package com.hh.game.service.gateway.manager.service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ServiceRouteConfigCache {
    /**
     * <serviceName ,<methodSign ,parameterTypes>>
     * <serviceName ,<methodSign_argsName ,parameterTypes>>
     *
     */
    private static Map<String ,Map<String ,String[]>> cache = new ConcurrentHashMap<>();

    public static void remove(String serviceName){
        cache.remove(serviceName);
    }

    public static void remove(String serviceName ,String methodSign){
        synchronized (serviceName.intern()) {
            Map<String, String[]> service = cache.get(serviceName);
            if(service != null){
                service.remove(methodSign);
                service.remove(methodSign + "_argsName");
            }
        }
    }
    
    public static void add(String serviceName ,String methodSign ,String[] parameterTypes){
        synchronized (serviceName.intern()) {
            Map<String, String[]> service = cache.get(serviceName);
            if (service == null) {
                service = new HashMap<>();
                cache.put(serviceName ,service);
            }
            service.put(methodSign , parameterTypes);
        }
    }
    public static void add(String serviceName ,String methodSign ,String[] parameterTypes ,String[] argsName){
        synchronized (serviceName.intern()) {
            Map<String, String[]> service = cache.get(serviceName);
            if (service == null) {
                service = new HashMap<>();
                cache.put(serviceName ,service);
            }
            service.put(methodSign , parameterTypes);
            service.put(methodSign + "_argsName" , argsName);
        }
    }

    public static String[] getParameterTypes(String serviceName ,String methodSign){
        Map<String, String[]> service = cache.get(serviceName);
        if (service != null) {
            return service.get(methodSign);
        }
        return null;
    }

    public static String[] getArgsName(String serviceName ,String methodSign){
        Map<String, String[]> service = cache.get(serviceName);
        if (service != null) {
            return service.get(methodSign + "_argsName");
        }
        return null;
    }


    public static String buildMethodSign(String handler ,String method){
        return handler + "." + method;
    }
}
